using System;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
namespace Wwo
{
    public class JianZhuView : Window
    {

        GList m_List;
        GButton m_MingLing;
        GButton m_JieGou;
        GButton m_JiaJu;
        GButton m_DiBan;
        GButton m_ShengChan;
        GButton m_QuYu;

        GButton m_CurrCmd;

        public JianZhuView()
        {
            UIObjectFactory.SetPackageItemExtension("ui://WaterWorld/CmdItem", typeof(CmdItem));
        }

        private void AddCmdBtn(string[] titles, string[] icons)
        {
            for (int i = 0; i < icons.Length; i++)
            {
                CmdItem item = (CmdItem)m_List.AddItemFromPool();
                item.icon = icons[i];
                item.title = titles[i];
                item.onClick.Add((EventContext context) =>
                {
                    context.PreventDefault();
                    // DragDropManager.inst.StartDrag(item, item.icon, item.icon);
                });
            }
        }

        private void onListClickItem(EventContext context)
        {
            CmdItem item = (CmdItem)context.data;
            if (item != null)
            {
                UISets.GetInstance().SetBuildItem(item.text);
            }
        }

        protected override void OnInit()
        {
            this.contentPane = UIPackage.CreateObject("WaterWorld", "JianZhuList").asCom;

            float y = GRoot.inst.height - this.contentPane.height - UISets.GetInstance().m_MainMenuView.height;
            this.SetXY(0, y);

            //
            m_MingLing = this.contentPane.GetChild("MingLing").asButton;
            m_JieGou = this.contentPane.GetChild("JieGou").asButton;
            m_JiaJu = this.contentPane.GetChild("JiaJu").asButton;
            m_DiBan = this.contentPane.GetChild("DiBan").asButton;
            m_ShengChan = this.contentPane.GetChild("ShengChan").asButton;
            m_QuYu = this.contentPane.GetChild("QuYu").asButton;

            m_List = this.contentPane.GetChild("listItems").asList;

            m_List.onClickItem.Add(onListClickItem);

            // 命令
            m_MingLing.onClick.Add(() =>
            {
                if (m_CurrCmd == m_MingLing)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_MingLing;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pbu",
                        "ui://sw31oo7zd42pbg",
                        "ui://sw31oo7zd42pb4"
                    };

                    string[] titles = {
                        "取消",
                        "拆除",
                        "狩猎",
                        "打捞"
                    };

                    AddCmdBtn(titles, icons);
                }
            });

            // 结构
            m_JieGou.onClick.Add(() =>
            {
                if (m_CurrCmd == m_JieGou)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_JieGou;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pbu",
                        "ui://sw31oo7zd42pb7",
                        "ui://sw31oo7zd42pbb",
                        "ui://sw31oo7zd42pb9"
                    };

                    string[] titles = {
                        "取消",
                        "拆除",
                        "地基",
                        "墙",
                        "门"
                    };

                    AddCmdBtn(titles, icons);
                }
            });

            // 家具
            m_JiaJu.onClick.Add(() =>
            {
                if (m_CurrCmd == m_JiaJu)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_JiaJu;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pbu",
                        "ui://sw31oo7zd42pbd",
                        "ui://sw31oo7zd42pbc",
                        "ui://sw31oo7zd42pb6",
                        "ui://sw31oo7zd42pb1",
                        "ui://sw31oo7zd42pba"
                    };

                    string[] titles = {
                        "取消",
                        "拆除",
                        "桌子",
                        "椅子",
                        "单人床",
                        "火把",
                        "木盆"
                    };

                    AddCmdBtn(titles, icons);
                }
            });

            // 地板
            m_DiBan.onClick.Add(() =>
            {
                if (m_CurrCmd == m_DiBan)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_DiBan;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pb7",
                        "ui://sw31oo7zd42pb7"
                    };

                    string[] titles = {
                        "取消",
                        "木地板",
                        "石地板"
                    };

                    AddCmdBtn(titles, icons);
                }
            });

            // 区域
            m_QuYu.onClick.Add(() =>
            {
                if (m_CurrCmd == m_QuYu)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_QuYu;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pbu"
                    };

                    string[] titles = {
                        "取消",
                        "拆除"
                    };

                    AddCmdBtn(titles, icons);
                }
            });


            // 生产
            m_ShengChan.onClick.Add(() =>
            {
                if (m_CurrCmd == m_ShengChan)
                {
                    m_CurrCmd = null;
                    m_List.visible = false;
                }
                else
                {
                    m_CurrCmd = m_ShengChan;
                    m_List.RemoveChildren();
                    m_List.visible = true;

                    string[] icons = {
                        "ui://sw31oo7zd42pbe",
                        "ui://sw31oo7zd42pbu"
                    };

                    string[] titles = {
                        "取消",
                        "拆除"
                    };

                    AddCmdBtn(titles, icons);
                }
            });

        }

        override protected void OnShown()
        {
        }
    }
}